package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.resource.GoodsResourceModule;
import com.yanqu.road.server.manger.SeacraftSouthSeaCommonMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/12 16:51
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_CHALLENGE, desc = " 海上争霸 南海丝路 挑战")
public class SeacraftSouthSeaChallengeCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaChallengeReqMsg reqMsg = SeacraftSouthSeaProto.SeacraftSouthSeaChallengeReqMsg.parseFrom(packet.getBytes());
        ret = SeacraftSouthSeaMgr.canEntryActivity(player,reqMsg.getActivityId());
        if(ret != 0){
            return;
        }
        // 判断是否在活动期
        if(!SeacraftSouthSeaCommonMgr.isOpenTime(SeacraftSouthSeaMgr.getActivityInfo(),SeacraftSouthSeaMgr.getConfig())){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_OPEN;
            return;
        }

        if(!SeacraftSouthSeaMgr.isPromotion(player.getUserInfo().getUnionUid())){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_TARGET_NO_PROMOTION;
            return;
        }
        long goldNum = 0;
        if(reqMsg.getType() == 2){
            // 一键挑战
            int vipLv = VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp());
            if(vipLv < SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_QUICKFIGHT_LIMIT_VIP()){
                ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
                return;
            }
            long time = SeacraftSouthSeaMgr.getActivityInfo().getEndTime() - SeacraftSouthSeaMgr.getConfig().getSEACRAFT_CRAFT_QUICKFIGHT_LIMIT_TIME_FINISH() * DateHelper.MINUTE_SECONDS;
            if(DateHelper.getCurrentSecond() > time){
                ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_END_CLOSE_NOT_ONE_KEY_FIGHT;
                return;
            }

            goldNum = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.Ingots);
            // 预扣除
            if(goldNum > 0){
                player.getModule(PreReduceModule.class).preReduce(new Property(GameConfig.GAME_MONEY_INGOTS, goldNum));
            }
        }
        SeacraftSouthSeaProto.SeacraftSouthSeaChallengeGameToCrossMsg.Builder gameToCross = SeacraftSouthSeaProto.SeacraftSouthSeaChallengeGameToCrossMsg.newBuilder();
        gameToCross.setActivityId(reqMsg.getActivityId());
        gameToCross.setCtsMsg(reqMsg);
        gameToCross.setGoldNum(goldNum);

        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_SOUTH_SEA_CHALLENGE, gameToCross, reqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SeacraftSouthSeaProto.SeacraftSouthSeaChallengeRespMsg.Builder resp = SeacraftSouthSeaProto.SeacraftSouthSeaChallengeRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_CHALLENGE,resp);
        }
    }
}
